import pylibczi
import pathlib
from PIL import Image
import numpy as np
pth = pathlib.Path('/allen/aics/assay-dev/MicroscopyData/Sue/2019/20190610/20190610_S02-02.czi')
pth.stat().st_size/(1024*1024*1024)
czi = pylibczi.CziFile(pth)
czi.dims()
img, shp = czi.read_image(S=4, T=11, C=0, Z=30)
shp
img.shape
d_img = img[0,0,0,0, :, :]
norm_by = np.percentile(d_img, [5, 98])
i2 = np.clip((d_img - norm_by[0])/(norm_by[1]-norm_by[0]), 0, 1)*255
d_img above will be of the integer type of the native image however after scalling and clipping the image needs to be cast back to an integer
img_disp = Image.fromarray(i2.astype(np.uint8))
img_disp
pTwo = pathlib.Path('~/Data/20190618_CL001_HB01_Rescan_002.czi').expanduser()
pTwo.stat().st_size/(1024*1024*1024)
cziTwo = pylibczi.CziFile(pTwo)
cziTwo.dims()
cziTwo.is_mosaic()
x, y is the upper left coorinate image based coordinate system
Note mosaic files as shown above have a Scene index but are not meant to be reassabled using that constraint they internally use an M-Index instead. Consequently when attempting to specify the 2D plane you want lock in all dimensions larger than 1 except scene. The scale_factor below loads a w/10 by h/10 version of the image (ie 1/100th of the original number of pixels).
icOne = cziTwo.read_mosaic(C=1, scale_factor=0.1)
icOne.shape
itwo = icOne[0,0,:, :]
norm_by = np.percentile(itwo, [5, 98])
itwonorm = np.clip((itwo - norm_by[0])/(norm_by[1]-norm_by[0]), 0, 1)*255
itwonorm_disp = Image.fromarray(itwonorm.astype(np.uint8))
itwonorm_disp